package com.lbb.waimai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbb.waimai.entiry.Admin;
import com.lbb.waimai.entiry.Category;
import com.lbb.waimai.mapper.AdminMapper;
import com.lbb.waimai.mapper.CategoryMapper;
import com.lbb.waimai.service.AdminService;
import com.lbb.waimai.service.CategoryService;
import com.lbb.waimai.utils.JwtHelper;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    //管理员登录
    @Override
    public String login(Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("name",admin.getName());
        wrapper.eq("pwd",admin.getPwd());
        Admin one = baseMapper.selectOne(wrapper);
        if (one == null){
            throw new RuntimeException("用户名或密码错误！");
        }
        Integer id = one.getId();
        long longid = Long.parseLong(id.toString());
        String token = JwtHelper.createToken(longid);
        return token;
    }

    //修改管理员密码
    @Override
    public void resetPwd(Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("name",admin.getName());
        Admin one = baseMapper.selectOne(wrapper);
        if (one == null){
            throw new RuntimeException("无该用户");
        }
        baseMapper.update(admin,wrapper);
    }
}
